package thread;

public class ThreadState {

    public static void main(String[] args) throws InterruptedException {
        Thread mainThread=Thread.currentThread();
        Thread thread=new Thread(()-> {
            while(true) {
                System.out.println("hello thread");
                try {
                    System.out.println("main state=" + mainThread.getState());
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        thread.start();
        thread.join();
    }

    //TIME_WAITING
    public static void main3(String[] args) throws InterruptedException {
        Thread thread=new Thread(()-> {
            System.out.println("hello thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        thread.start();
        Thread.sleep(500);
        System.out.println(thread.getState());
    }

    public static void main2(String[] args) throws InterruptedException {
        Thread thread=new Thread(()-> {
        });
        System.out.println(thread.getState());
        thread.start();
        Thread.sleep(1000);
        System.out.println(thread.getState());
    }
    public static void main1(String[] args) {
        Thread mainThread=Thread.currentThread();//获取主线程的引用
        Thread thread=new Thread(()-> {
            System.out.println("hello thread");
            try {
                mainThread.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);//让thread线程等待main线程
            }
        });
        System.out.println(thread.getState());
        thread.start();
        System.out.println(thread.getState());

    }
}
